package com.lhnstart.learn.bases.polymorphism;

/**
 * 多态案例题
 *
 * @author lhn
 * @date 2022/11/12 19:19
 * @since 1.0.0
 **/
public class PolymorphismDemo {
    public static void main(String[] args) {
        System.out.println(new B().getValue());//17
    }

    static class A {
        protected int value;//10 22 16 34

        public A(int v) {// 5
            setValue(v);
        }

        public int getValue() {
            try {
                value++;
                return value;//11  17
            } finally {
                this.setValue(value);//11 17
                System.out.println(value);//22 34
            }
        }

        public void setValue(int value) { // 10 22 16 34
            this.value = value;
        }
    }

    static class B extends A {
        public B() {
            super(5);
            setValue(getValue() - 3);//11-3=8
        }

        public void setValue(int value) { // 5 11 8 17
            super.setValue(2 * value);// 2*8=16  2*17=34
        }
    }
}